import { ATeamCombat } from "../TeamCombat";

export class AttackStrategy
{
    private static attackAroundStructures(creep:Creep)
    {
        if(creep.room.IsMy())
        {
            // 或者是友方
            return false;
        }
        const cachestructure=  creep.room.getWorld().PollStructure(creep.pos.roomName);
        if(cachestructure)
        {
            const structures = cachestructure.findInRange(creep.pos,1).filter(e=>e.structureType!== STRUCTURE_ROAD  && e.structureType!== STRUCTURE_CONTAINER);

            if(structures.length==0)
            {
                return false;
            }
            for(const e of structures)
            {
                const distance = creep.pos.distance(e.pos);
                if(distance<=1   )
                {
                    creep.attack(e);
                    return true;
                }
            }
        }
        return false;
    }
    private static attackAroundCreep(e:Creep,team:ATeamCombat)
    {
        let findcreeps = team.findHostilesCreeps();
        findcreeps = _.sortBy(findcreeps,e=>e.getActiveBodyparts(TOUGH));
        for(const host of findcreeps)
        {
             
            if(e.pos.distance(host.pos)<=1)
            {
                e.attack(host);
                return true;
            }
             
        }
        return false;
    }
     
    /**
     * 从target中选取歼灭目标，和骚扰目标进行攻击
 */
    
    public static attack(team:ATeamCombat,targets:string[])
    {
         
        const attacker = team.getAttacker();
        for(const item of attacker)
        {
            if(this.attackTargets(item,targets as Id<Structure>[]))
            {
               continue;
            }
            if(this.attackAroundCreep(item,team))
            {
                continue;
            }
            this.attackAroundStructures(item);
        }
    }
    private static attackTargets(creep:Creep,targets:Id<Structure>[])
    {
        const host = Game.getObjectById(targets[0] as Id<Structure>) as Structure;
        if(!host)
        {
            return false;
        }
        {
            if(creep.pos.distance(host.pos)<=1)
            {
                creep.attack(host);
                return true;
            }
        }
        return false;
    }
}